/**
 * Created with JetBrains PhpStorm.
 * User: vander
 * Date: 5/3/13
 * Time: 11:16 PM
 * To change this template use File | Settings | File Templates.
 */
var MyTools = new Object();

MyTools.hexDigits = new Array("0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f");

MyTools.hex = function hex(x) {
    return isNaN(x) ? "00" : MyTools.hexDigits[(x - x % 16) / 16] + MyTools.hexDigits[x % 16];
};

MyTools.rgb2hex = function rgb2hex(rgb) {
    rgb = rgb.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);
    return "#" + MyTools.hex(rgb[1]) + MyTools.hex(rgb[2]) + MyTools.hex(rgb[3]);
};

MyTools.saveLocally = function(key, value){
    localStorage[key] = value;
}

MyTools.loadLocally = function(key){
    return localStorage[key];
}

MyTools.saveToDisk = function(fileURL, fileName) {
    // for non-IE
    if (!window.ActiveXObject) {
        var save = document.createElement('a');
        save.href = fileURL;
        save.target = '_blank';
        save.download = fileName || 'unknown';

        var event = document.createEvent('Event');
        event.initEvent('click', true, true);
        save.dispatchEvent(event);
        (window.URL || window.webkitURL).revokeObjectURL(save.href);
    }

    // for IE
    else if ( !! window.ActiveXObject && document.execCommand)     {
        var _window = window.open(fileURL, '_blank');
        _window.document.close();
        _window.document.execCommand('SaveAs', true, fileName || fileURL)
        _window.close();
    }
}

MyTools.viewThumbnail = function(pageId){
    window.setTimeout(function(){
        var tem = $j("#"+pageId).clone();
        $j(tem).css("display", "block");

        $j("#" + Static.containerPagePreview).find("li[pageId=" + pageId + "]").find("div.item").empty()
            .append(tem).find("*[id]").attr("id", "");
    }, 150);
};

